package jdk18.entity;

import jdk.nashorn.internal.objects.annotations.Setter;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Description
 * @Author zhongzihao
 * @Date 2021/9/10 11:54 上午
 */

public class Dish {
    private String id;

    private String name;
    private Date tiem;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Date getTiem() {
        return tiem;
    }

    public void setTiem(Date tiem) {
        this.tiem = tiem;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Dish(String id, String name, Date tiem) {
        this.id = id;
        this.name = name;
        this.tiem = tiem;
    }


    public static List<Dish> getDishs() throws Exception {
        List<Dish> dishList = new ArrayList<Dish>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        Date s1 = sdf.parse("2021-6-21 00:00:00");
        Dish dish1 = new Dish("001", "张三", sdf.parse("2021-6-21 00:00:00"));
        dishList.add(dish1);
        Dish dish0 = new Dish("000", "sada三", sdf.parse("2021-6-21 10:00:00"));
        dishList.add(dish0);
        Dish dish2 = new Dish("001", "李四", sdf.parse("2021-6-22 00:00:00"));
        dishList.add(dish2);
        Dish dish01 = new Dish("003", "sada", sdf.parse("2021-6-21 10:01:00"));
        dishList.add(dish01);
        Dish dish02 = new Dish("003", "sasdadfsdfda", sdf.parse("2021-6-23 10:01:00"));
        dishList.add(dish02);
        Dish dish3 = new Dish("002", "王五", sdf.parse("2021-6-20 00:00:00"));
        Dish dish4 = new Dish("002", "李四sda", sdf.parse("2021-6-24 00:00:00"));
        dishList.add(dish3);
        dishList.add(dish4);
        return dishList;
    }

    /**
     * 测试Collectors.collectingAndThen方法：将流中的数据通过Collector计算，计算的结果再通过Function处理一下
     * (这里是将TreeSet转为ArrayList。即相当于将最终结果又经过了new ArrayList<>(treeSet))
     */
    @SuppressWarnings("unchecked")
    public static void testCollectingAndThen() throws Exception{
        List<Dish> newDishList = getDishs().stream().collect(Collectors.collectingAndThen(
                Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Dish::getId))), ArrayList::new));
        newDishList.forEach(d -> System.out.println("id:" + d.getId() + ", name:" + d.getName()+", time:" + d.getTiem()));
        // id:001, name:张三
        // id:002, name:王五
    }
    @SuppressWarnings("unchecked")
    public static void testCollectingAndThen2() throws Exception{

        List<Dish> newDishList = getDishs().stream().sorted(Comparator.comparing(Dish::getTiem).reversed()).collect(Collectors.toList());
        newDishList.forEach(d -> System.out.println("id:" + d.getId() + ", name:" + d.getName()+", time:" + d.getTiem()));
        List<Dish> newDishList2 = newDishList.stream().collect(
                Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Dish::getId))), ArrayList::new)
        );
        newDishList2.forEach(d -> System.err.println("id:" + d.getId() + ", name:" + d.getName()+", time:" + d.getTiem()));
        // id:001, name:张三
        // id:002, name:王五
    }
    public static void test2() throws Exception{
        List<Dish> newDishList  = getDishs().stream().sorted(
                Comparator.comparing(Dish::getTiem).reversed()
        ).collect(
                Collectors.collectingAndThen(Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(Dish::getId))), ArrayList::new)
        );
        newDishList.forEach(d -> System.out.println("test2 id:" + d.getId() + ", name:" + d.getName()+", time:" + d.getTiem()));
    }

    public static void main(String[] args) throws Exception {
//        testCollectingAndThen2();
//        test2();
//        Integer i = null;
        System.err.println(StringUtils.equals(null,"112"));
    }
}
